//
//  ByViewGoodsCondition.m
//  mijifen_ios
//
//  Created by mc on 2017/11/1.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewGoodsCondition.h"

#import "ByIconChooseDataSource.h"

@interface ByViewGoodsCondition()<UIActionSheetDelegate,UITextFieldDelegate>
// other
@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource;

@property (nonatomic, assign) NSInteger preTag;
@property (nonatomic, strong) NSMutableDictionary *infoDic;
@end

@implementation ByViewGoodsCondition
{
    UIImageView     *_goodImg;
    UIButton        *_sameDayButton;
    UIButton        *_freeButton;
    
    UILabel         *_buyLeftLabel;
    UILabel         *_buyRightLabel;
    UITextField     *_distanceTf;
    UIView          *_lineView;
    
    UIView          *_pickBgView;
    UILabel         *_pickLabel;
    UITextField     *_pickTf;
    UILabel         *_pickFlagLabel;
    
    UIView          *_indexBgView;
    UILabel         *_indexLabel;
    UISwitch        *_indexSwitch;

    UILabel         *_pickAlertLabel;
    UILabel         *_indexAlertLabel;

}
- (instancetype)init{
    if (self = [super init]) {
        [self createViews];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

#pragma mark - getter
- (ByIconChooseDataSource *)iconChooseDataSource {
    if (!_iconChooseDataSource) {
        _iconChooseDataSource = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource fireWithViewController:self.viewController];
    }
    return _iconChooseDataSource;
}

- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}

- (void)createViews{
    _goodImg = [[UIImageView alloc] init];
    [self addSubview:_goodImg];
    [_goodImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(140, 140));
    }];
    _goodImg.image = [UIImage imageNamed:@"Index_photo"];
    _goodImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoAction)];
    [_goodImg addGestureRecognizer:tap];
    
    // 当日达
    _sameDayButton = [[UIButton alloc] init];
    [self addSubview:_sameDayButton];
    CGFloat width = [@"当日达（平台配送）" getSizeOfString:16].width + 30;
    [_sameDayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_goodImg.mas_top).offset(20);
        make.left.equalTo(_goodImg.mas_right).offset(15);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    [_sameDayButton setTitle:@"当日达（平台配送）" forState:UIControlStateNormal];
    [_sameDayButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_sameDayButton setImage:[UIImage imageNamed:@"Index_unSelected"] forState:UIControlStateNormal];
    [_sameDayButton setImage:[UIImage imageNamed:@"Index_selected"] forState:UIControlStateSelected];
    _sameDayButton.selected = YES;
    _sameDayButton.titleLabel.font = FONT(16);
    _sameDayButton.tag = self.preTag = 10;
    
    // 商家配送
    _freeButton = [[UIButton alloc] init];
    [self addSubview:_freeButton];
    width = [@"商家免费配送" getSizeOfString:16].width + 30;
    [_freeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_sameDayButton.mas_left);
        make.top.equalTo(_sameDayButton.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    [_freeButton setTitle:@"商家免费配送" forState:UIControlStateNormal];
    [_freeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_freeButton setImage:[UIImage imageNamed:@"Index_unSelected"] forState:UIControlStateNormal];
    [_freeButton setImage:[UIImage imageNamed:@"Index_selected"] forState:UIControlStateSelected];
    _freeButton.titleLabel.font = FONT(16);
    _freeButton.tag = 11;
    
    [_freeButton addTarget:self action:@selector(sendTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [_sameDayButton addTarget:self action:@selector(sendTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 买家距离您
    width = [@"（选填）买家距离您" getSizeOfString:16].width + 5;
    _buyLeftLabel = [[UILabel alloc] init];
    [self addSubview:_buyLeftLabel];
    [_buyLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(_goodImg.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    _buyLeftLabel.text = @"（选填）买家距离您";
    _buyLeftLabel.font = FONT(16);
    
    width = [@"米之内送货上门" getSizeOfString:16].width + 5;
    _buyRightLabel = [[UILabel alloc] init];
    [self addSubview:_buyRightLabel];
    [_buyRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-10);
        make.top.equalTo(_buyLeftLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    _buyRightLabel.text = @"米之内送货上门";
    _buyRightLabel.font = FONT(16);
    
    _distanceTf = [[UITextField alloc] init];
    [self addSubview:_distanceTf];
    [_distanceTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_buyLeftLabel.mas_right);
        make.right.equalTo(_buyRightLabel.mas_left);
        make.top.equalTo(_buyLeftLabel.mas_top);
        make.height.mas_equalTo(@(20));
    }];
    _distanceTf.font = FONT(16);
    _distanceTf.textAlignment = NSTextAlignmentRight;
    _distanceTf.delegate = self;
    _distanceTf.tag = 12;
    
    _lineView = [[UIView alloc] init];
    [self addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_distanceTf.mas_left);
        make.bottom.equalTo(_distanceTf.mas_bottom);
        make.right.equalTo(_distanceTf.mas_right);
        make.height.mas_equalTo(@(1));
    }];
    _lineView.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    
    
    // 自取立减
    _pickBgView = [[UIView alloc] init];
    [self addSubview:_pickBgView];
    [_pickBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_buyLeftLabel.mas_bottom).offset(10);
        make.left.equalTo(self.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - 30) / 2, 50));
    }];
    _pickBgView.backgroundColor = [UIColor whiteColor];
    
    width = [@"自取立减：" getSizeOfString:16].width + 5;
    _pickLabel = [[UILabel alloc] init];
    [_pickBgView addSubview:_pickLabel];
    [_pickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_pickBgView.mas_left).offset(10);
        make.top.equalTo(_pickBgView.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _pickLabel.font = FONT(16);
    _pickLabel.text = @"自取立减：";
    
    width = [@"¥" getSizeOfString:16].width + 5;
    _pickFlagLabel = [[UILabel alloc] init];
    [_pickBgView addSubview:_pickFlagLabel];
    [_pickFlagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_pickBgView.mas_right).offset(-10);
        make.top.equalTo(_pickLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _pickFlagLabel.text = @"¥";
    _pickFlagLabel.font = FONT(16);
    
    _pickTf = [[UITextField alloc] init];
    [_pickBgView addSubview:_pickTf];
    [_pickTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_pickLabel.mas_right);
        make.top.equalTo(_pickLabel.mas_top);
        make.right.equalTo(_pickFlagLabel.mas_left);
        make.height.mas_equalTo(@(30));
    }];
    _pickTf.font = FONT(16);
    _pickTf.textAlignment = NSTextAlignmentRight;
    _pickTf.delegate = self;
    _pickTf.tag = 13;
    
    // 首页推荐
    _indexBgView = [[UIView alloc] init];
    [self addSubview:_indexBgView];
    [_indexBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_pickBgView.mas_right).offset(10);
        make.top.equalTo(_pickBgView.mas_top);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(@(50));
    }];
    _indexBgView.backgroundColor = [UIColor whiteColor];
    
    width = [@"首页推荐：" getSizeOfString:16].width + 5;
    _indexLabel = [[UILabel alloc] init];
    [_indexBgView addSubview:_indexLabel];
    [_indexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_indexBgView.mas_left).offset(10);
        make.top.equalTo(_indexBgView.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _indexLabel.text = @"首页推荐";
    
    _indexSwitch = [[UISwitch alloc] init];
    [_indexBgView addSubview:_indexSwitch];
    [_indexSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_indexBgView.mas_right).offset(-10);
        make.top.equalTo(_indexBgView.mas_top).offset(10);
        make.left.equalTo(_indexLabel.mas_right).offset(10);
        make.height.mas_equalTo(@(30));
    }];
    _indexSwitch.thumbTintColor = BY_TEXT_COLOR;
    _indexSwitch.onTintColor = BY_THEME_COLOR;
    [_indexSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    width = [@"自取可吸引客户到店消费哦！" getSizeOfString:14].width + 5;
    _pickAlertLabel = [[UILabel alloc] init];
    [self addSubview:_pickAlertLabel];
    [_pickAlertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(_pickBgView.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    _pickAlertLabel.text = @"自取可吸引客户到店消费哦！";
    _pickAlertLabel.font = FONT(14);
    _pickAlertLabel.textColor = [UIColor redColor];
    
    _indexAlertLabel = [[UILabel alloc] init];
    [self addSubview:_indexAlertLabel];
    [_indexAlertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-10);
        make.top.equalTo(_pickAlertLabel.mas_top);
        make.left.equalTo(_pickAlertLabel.mas_right).offset(5);
        make.height.mas_equalTo(@(20));
    }];
    _indexAlertLabel.textAlignment = NSTextAlignmentRight;
    _indexAlertLabel.font = FONT(14);
    _indexAlertLabel.textColor = BY_TEXT_COLOR;
    _indexAlertLabel.text = @"开通首页推荐";
    
}

#pragma mark - 相关事件
// 拍照
- (void)photoAction{
    [self.iconChooseDataSource makeConfigToSelectIcon];
    __weak typeof(_goodImg) weakImg = _goodImg;
    __weak typeof(self) weakSelf = self;
    [self.iconChooseDataSource setCompleteBlock:^(UIImage *image) {
        weakImg.image = image;
        if (weakSelf.photoBlock) {
            weakSelf.photoBlock(image);
        }
//        [weakself uploadIcon:image];
    }];
}

// 配送方式
- (void)sendTypeAction:(UIButton *) button{
    if (self.preTag == button.tag) {
        return;
    }
    
    UIButton *preButton = [self viewWithTag:self.preTag];
    preButton.selected = NO;
    
    button.selected = YES;
    self.preTag = button.tag;
    
    if (self.sendBlock) {
        self.sendBlock(button.tag - 10);
    }
}

// 编辑
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag == 12) {
        [self.infoDic setValue:textField.text forKey:@"distance"];
    }else{
        [self.infoDic setValue:textField.text forKey:@"pickPrice"];
    }
    if (self.editEndBlock) {
        self.editEndBlock(self.infoDic);
    }
}

// 首页推荐
- (void)switchAction:(UISwitch *)indexSwitch{
    if (self.recommendBlock) {
        self.recommendBlock(indexSwitch.selected);
    }
}

@end
